package DesignModeTest;

/**
 * 适配器模式
 */
public class TestAdapter {

  public static void main(String[] args) {
    AudioPlayer audioPlayer = new AudioPlayer();
    audioPlayer.play("mp3", "海阔天空");
    audioPlayer.play("vlc", "far away.vlc");
    audioPlayer.play("mp4", "七个小矮人.mp4");
    audioPlayer.play("avi", "学习资料.avi");
  }

}


interface MediaPlayer{
  public void play(String audioType, String fileName);
}

interface  AdvancedMediaPlayer{
  void playVlc(String fileName);
  void playMp4(String fileName);
}

class VlcPlayer implements  AdvancedMediaPlayer{

  @Override
  public void playVlc(String fileName) {
    System.out.println("playVlc fileName == " + fileName);
  }

  @Override
  public void playMp4(String fileName) {
    // 什么也不做
  }
}

class Mp4Player implements AdvancedMediaPlayer {

  @Override
  public void playVlc(String fileName) {
    // 什么也不做
  }

  @Override
  public void playMp4(String fileName) {
    System.out.println("playVlc playMp4 == " + fileName);
  }
}

class MediaAdapter implements MediaPlayer{
  AdvancedMediaPlayer advancedMediaPlayer;

  public MediaAdapter(String audioType) {
    if(audioType.equals("vlc")) {
      advancedMediaPlayer  = new VlcPlayer();
    }else if(audioType.equals("mp4")){
      advancedMediaPlayer = new Mp4Player();
    }
  }

  @Override
  public void play(String audioType, String fileName) {
    if(audioType.equals("vlc")) {
      advancedMediaPlayer.playVlc(fileName);
    }else if(audioType.equals("mp4")){
      advancedMediaPlayer.playMp4(fileName);
    }
  }
}

class AudioPlayer implements MediaPlayer{

  @Override
  public void play(String audioType, String fileName) {
    //播放 mp3 音乐文件的内置支持
    if(audioType.equalsIgnoreCase("mp3")){
      System.out.println("Playing mp3 file. Name: "+ fileName);
    }else if(audioType.equals("vlc") || audioType.equals("mp4")) {
      //mediaAdapter 提供了播放其他文件格式的支持
      MediaAdapter mediaAdapter = new MediaAdapter(audioType);
      mediaAdapter.play(audioType, fileName);
    }else{
      System.out.println("Invalid media. "+
          audioType + " format not supported");
    }
  }
}